// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
package tc.map;

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;

//=============================================================================
/**Partial tile with image.
*/
public class TcTile {
//=============================================================================	
public final static int TILE_PIXELS = 256;

private static BufferedImage imageFake = null;
private TcTileAddress address;
private Image image;

TcTile(int layer, int hor, int ver) {
	if (imageFake == null) {
		imageFake = new BufferedImage( TILE_PIXELS, TILE_PIXELS, BufferedImage.TYPE_INT_ARGB );
		Graphics g = imageFake.getGraphics();
		g.setColor(Color.BLACK);
		g.drawLine( TILE_PIXELS/4,  TILE_PIXELS/4, 3*TILE_PIXELS/4, 3*TILE_PIXELS/4);
		g.drawLine( TILE_PIXELS/4,  3*TILE_PIXELS/4, 3*TILE_PIXELS/4, TILE_PIXELS/4);
		g.drawLine( 0,  0, TILE_PIXELS,  0);
		g.drawLine( TILE_PIXELS,  0,  TILE_PIXELS, TILE_PIXELS);
		g.drawLine( TILE_PIXELS, TILE_PIXELS,  0, TILE_PIXELS);
		g.drawLine( 0, TILE_PIXELS, 0, 0);
	}
	
	address = new TcTileAddress(layer, hor, ver);

    image = null;
}

//=============================================================================
public synchronized void updateImage(Image image) {
	this.image = image; 
}

public TcTileAddress getAddress() {
	return address; //Make address immutable
}

public synchronized Image getImage() {
	if (image == null) return imageFake;
	return image;
}


public void draw( Graphics g, int left, int top, int horOrigin, int verOrigin ) {
	int right = left + 256;
	int bot = top + 256;
	
	g.drawImage(getImage(), left, top, 256, 256, null  );
	
	if ( TcMviewControl.DEBUG_DRAWING == false ) return;
	
	g.drawString(address.toString(), left+10, top+50);
	g.drawLine( left,  top,    right, top);	
	g.drawLine( right, top,    right, bot);
	g.drawLine( right, bot, left,  bot);
	g.drawLine( left,  bot, left,  top);
	
	if ( address.getHor() != horOrigin ) return;
	if ( address.getVer() != verOrigin ) return;
	
	g.drawLine( left,  bot, right,  top);
	g.drawLine( left,  top, right,  bot);
}
//=============================================================================
}
